﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting;
using System.Collections;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Remoting.Channels.Http;

namespace Learn.Remoting.Server
{
    class Program
    {
        static void Main(string[] args)
        {
            //System.Runtime.Remoting.RemotingConfiguration.
            //RemotingConfiguration.Configure("Learn.Remoting.Server.exe.config");
            //Console.ReadLine();

            //RegisterWellKnownService();

            RegisterActivatedService();
            
        }

        public static void RegisterWellKnownService()
        {
            /// Create a 'HttpChannel' object and register it with the 
            // channel services.
            ChannelServices.RegisterChannel(new HttpChannel(1234));
            // Record the 'HelloServer' type as 'Singleton' well-known type.
            WellKnownServiceTypeEntry myWellKnownServiceTypeEntry =
                new WellKnownServiceTypeEntry(typeof(MyRemotingHelloServer),
                                              "SayHello",
                                              WellKnownObjectMode.Singleton);
            WellKnownServiceTypeEntry myWellKnownServiceTypeEntry2 =
                new WellKnownServiceTypeEntry(typeof(MyRemoteObject),
                                              "MyRemoteObject",
                                              WellKnownObjectMode.SingleCall);
            // Register the remote object as well-known type.
            RemotingConfiguration.RegisterWellKnownServiceType(
                                                myWellKnownServiceTypeEntry);
            RemotingConfiguration.RegisterWellKnownServiceType(
                                               myWellKnownServiceTypeEntry2);
            // Retrieve object types registered on the service end 
            // as well-known types.
            WellKnownServiceTypeEntry[] myWellKnownServiceTypeEntryCollection =
                  RemotingConfiguration.GetRegisteredWellKnownServiceTypes();
            Console.WriteLine("The 'WellKnownObjectMode' of the remote object : "
                             + myWellKnownServiceTypeEntryCollection[0].Mode);
            Console.WriteLine("The 'WellKnownServiceTypeEntry' object: " +
                        myWellKnownServiceTypeEntryCollection[0].ToString());
            Console.WriteLine("Started the Server, Hit <enter> to exit...");
            Console.ReadLine();

        }

        public static void RegisterActivatedService()
        {
            ChannelServices.RegisterChannel(new TcpChannel(4321));
            // Create an instance of 'ActivatedServiceTypeEntry' class
            // which holds the values for 'HelloServer' type.
            ActivatedServiceTypeEntry myActivatedServiceTypeEntry =
                         new ActivatedServiceTypeEntry(typeof(MyRemotingHelloServer));
            // Register an object Type on the service end so that 
            // it can be activated on request from a client.
            RemotingConfiguration.RegisterActivatedServiceType(
                                               myActivatedServiceTypeEntry);
            // Get the registered activated service types .
            ActivatedServiceTypeEntry[] myActivatedServiceEntries =
                RemotingConfiguration.GetRegisteredActivatedServiceTypes();
            Console.WriteLine("Information of first registered activated "
                                   + " service type :");
            Console.WriteLine("Object type: "
                             + myActivatedServiceEntries[0].ObjectType);
            Console.WriteLine("Description: "
                                 + myActivatedServiceEntries[0].ToString());
            Console.WriteLine("Press enter to stop this process");
            Console.ReadLine();

        }
    }
}
